import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/modle/person/bloc/my_tuanyou_bloc.dart';
import 'package:shopping_group/modle/person/entity/user_friends_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

import 'bank_list_page.dart';
import 'package:shopping_group/modle/person/entity/userid_name_phone_entity.dart';

import 'package:shopping_group/modle/person/ui/down_tuanyou_page.dart';

class ChangeDaiLiPage extends StatefulWidget {
  @override
  _ChangeDaiLiPageState createState() => _ChangeDaiLiPageState();
}

class _ChangeDaiLiPageState extends State<ChangeDaiLiPage>
    with AutomaticKeepAliveClientMixin {
  UserFriendsEntity _entity = UserFriendsEntity();

  MyTuanYouBloc _bloc;
  List<Widget> listViews = [
    //  UpTuanYouPage(),
    // DownTuanYouPage(),
  ];

  // List<String> listStr = [
  //   '同群客户',
  //   '下级客户',
  // ];
  int _index = 0;

  StreamSubscription _refresSubscription;
  StreamSubscription _upSubscription;
  StreamSubscription _downSubscription;
  @override
  void initState() {
    super.initState();
    _bloc = MyTuanYouBloc();
    _bloc.initDate.add(null);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '我的团友',
          textColor: Colors.white,
          isHide: false,
          backgroundColor: Colours.blackground_609, onTapCall: (e) {
        RouteUtil.goParameter(BankListPage());
      }),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: _allView(),
          ),
        ),
      ),
    );
  }

  Widget _allView() {
    return StreamBuilder(
      stream: _bloc.sendAllData,
      initialData: _entity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is UserFriendsEntity) {
          _entity = object;
        }
        if (!ObjectUtil.isEmptyList(_entity.lstChild)) {
          listViews.add(DownTuanYouPage(
            downlistEntity: _entity.lstChild,
          ));
        }
        return Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Expanded(
                flex: 1,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colours.blackground_609,
                  ),
                  child: Stack(
                    children: <Widget>[
                      Container(
                        alignment: Alignment.centerLeft,
                        padding:
                            EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _moneyOneView(),
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
            SizedBox(
              height: 8,
            ),
            Expanded(
              child: bodyView(),
              flex: 3,
            ),
            SizedBox(
              height: 8,
            ),
          ],
        );
      },
    );
  }

  Widget _moneyOneView() {
    return Expanded(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Container(
          color: Colours.transparent,
          alignment: Alignment.centerLeft,
          child: Row(
            children: <Widget>[
              WidgetUtils.getTextColorView(
                '我的市场顾问:',
                Colours.title_white,
                Dimens.FONT_SIZE_16,
              ),
              // SizedBox(
              //   width: 8,
              // ),
              // WidgetUtils.getTextColorView(
              //   null == _entity.upCust ||
              //           ObjectUtil.isEmptyString(_entity.upCust.phone)
              //       ? ''
              //       : _entity.upCust.phone,
              //   Colours.title_white,
              //   Dimens.FONT_SIZE_16,
              // ),
            ],
          ),
        ),
        SizedBox(
          height: 16,
        ),
        Expanded(
          child: Container(
              alignment: Alignment.center,
              child: Column(
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                    null == _entity.upCust ||
                            ObjectUtil.isEmptyString(_entity.upCust.name)
                        ? '暂无'
                        : _entity.upCust.name,
                    Colours.title_white,
                    Dimens.FONT_SIZE_28,
                  ),
                  SizedBox(
                    height: 4,
                  ),
                  WidgetUtils.getTextColorView(
                    null == _entity.upCust ||
                            ObjectUtil.isEmptyString(_entity.upCust.tuiGuanFei)
                        ? ''
                        : '¥ ${_entity.upCust.tuiGuanFei}',
                    Colours.title_white,
                    Dimens.FONT_SIZE_16,
                  ),
                  SizedBox(
                    height: 4,
                  ),
                  WidgetUtils.getTextColorView(
                    null == _entity.upCust ||
                            ObjectUtil.isEmptyString(_entity.upCust.phone)
                        ? ''
                        : _entity.upCust.phone,
                    Colours.title_white,
                    Dimens.FONT_SIZE_16,
                  ),
                ],
              )),
        ),
      ],
    ));
  }

  Widget bodyView() {
    return Container(
        color: Colours.title_white,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            Container(
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(top: 12, left: 16),
              child: WidgetUtils.getTextColorView(
                  '我的市场团队', Colours.text_2222, Dimens.FONT_SIZE_16,
                  fontweight: FontWeight.w500),
            ),
            //  changeBottomView(),
            Expanded(
              child: ObjectUtil.isEmptyList(listViews)
                  ? Container()
                  : listViews[_index],
            ),
          ],
        ));
  }

  // Widget changeBottomView() {
  //   return Container(
  //     color: Colors.white,
  //     alignment: Alignment.center,
  //     padding: const EdgeInsets.only(top: 12),
  //     child: Row(
  //       crossAxisAlignment: CrossAxisAlignment.center,
  //       mainAxisAlignment: MainAxisAlignment.center,
  //       children: <Widget>[
  //         Expanded(
  //           child: Row(
  //             crossAxisAlignment: CrossAxisAlignment.center,
  //             mainAxisAlignment: MainAxisAlignment.center,
  //             children: [
  //               showChangeView(
  //                 0,
  //                 listStr[0],
  //                 _index == 0 ? Colours.blackground_609 : Colours.text_2222,
  //                 _index == 0 ? Colours.blackground_609 : Colors.transparent,
  //               ),
  //               showChangeView(
  //                 1,
  //                 listStr[1],
  //                 _index == 1 ? Colours.blackground_609 : Colours.text_2222,
  //                 _index == 1 ? Colours.blackground_609 : Colors.transparent,
  //               ),
  //             ],
  //           ),
  //         ),
  //       ],
  //     ),
  //   );
  // }

  Widget showChangeView(
      int index, String title, Color color, Color dividerColor) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          setState(() {
            _index = index;
          });
        },
        child: Container(
          alignment: Alignment.center,
          color: Colors.transparent,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                      title, color, Dimens.FONT_SIZE_14),
                  Container(
                    padding: const EdgeInsets.only(top: 12),
                    //margin: EdgeInsets.symmetric(horizontal: 56),
                    width: 64,
                    child: WidgetUtils.drawCodeDivider(
                        color: dividerColor, height: 2),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _refresSubscription?.cancel();
    _upSubscription?.cancel();
    _downSubscription?.cancel();
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
